var url = "/openx/wms/cityStoreService/queryCityStoreForPage";
var queryForm = 'queryForm';

var ownOptions={};
/**查询参数信息*/
//对应接口入参
var param = {};

var formId = 'queryForm';

//匹配门店编码的正则表达式

var storeCodeRegex = /^([a-zA-Z0-9]{4})(,[a-zA-Z0-9]{4})*$/;

(function () {
//        /**设置增删改查的工具条*/
    $.openx.setTableToolBar('partnerbar');
    /**进行表格数据初始化*/
    var data = $.openx.getJsonObj(formId);
    param.bean = data;
    $.openx.initTable("table", url, param);
    initAreaSelect("province", "city", "district");
})();


$.openx.insert = function () {
	
    var createForm = "addCityStoreForm";
    var innerHtml = $.openx.innerHtml('addCityStoreDialog');
    laytpl(innerHtml).render({}, function (html) {
        $.openx.openLayer(['auto', '400'], '新增城市门店信息管理', html, false, function () {
        	var bean = $.openx.getJsonObj(createForm);
        	if ($.openx.validation(createForm)) {
                //获取表单数据
                var provinceName = $("#add_province option:selected").text();
                
                bean.provinceName = provinceName;
                var cityName = $("#add_city option:selected").text();
                if ($("#add_city option:selected").val().trim().length === 0) {
                    layer.tips('所属城市不能为空', '#add_city_tips', {
                      tips: [2, '#ff4400']
                    });
                    return false;
                }
                bean.cityName = cityName;
                bean.receiveAreaName = provinceName + "-" + cityName;
                //重新对页面的门店编码文本域赋值（因在获取参数时有字符转换）
                $("#storeCodes").val(bean.storeCodes);
                bean.storeCodes = bean.storeCodes.split(",");
                if ($("#storeCodes").val().trim().length === 0) {
            		layer.tips('门店编码不能为空', '#add_store_codes_tips', {
	                      tips: [2, '#ff4400']
	                    });
	                    return false;
            	}
            	if (!storeCodeRegex.test($("#storeCodes").val())){
                    layer.tips('门店编码格式不正确', '#add_store_codes_tips', {
                      tips: [2, '#ff4400']
                    });
                    return false;
                }
                console.log(bean);
                
                $X("/wms/cityStoreService", "saveCityStore").callx({
                	bean: bean,
                    onResult: function (data) {
                    	if(data){
                    		$("#errorMessage").html(data);
                    		$.openx.tableRefresh(null, null, true);
                    		if(data.length==1){
                    			layer.closeAll();
                    		}
//                    		layer.closeAll();
//                    		layer.msg(data,{time:6000});
                    	}
                           
                    },
                    onError: function (error) {
                        var res = JSON.parse(error);
                        layer.msg(res.message)
                    }
                });
            }
        });
    });
    initAreaSelect("add_province", "add_city", "district");

}



$.openx.update = function () {
    $.openx.onClickUpdate(function (row) {
    	
        var updateForm = "addCityStoreForm";
        var innerHtml = $.openx.innerHtml('addCityStoreDialog');
        laytpl(innerHtml).render(row[0], function (html) {
            $.openx.openLayer(['auto', '400'], '修改屏蔽管理信息', html, false, function () {
            	var cityStore = $.openx.getJsonObj(updateForm);
            	if ($.openx.validation(updateForm)) {
                    //获取表单数据
            		 var provinceName = $("#add_province option:selected").text();
                     cityStore.provinceName = provinceName;
                     var cityName = $("#add_city option:selected").text();
                     if ($("#add_city option:selected").val().trim().length === 0) {
                         layer.tips('所属城市不能为空', '#add_city_tips', {
                           tips: [2, '#ff4400']
                         });
                         return false;
                     }
                     cityStore.cityName = cityName;
                     cityStore.receiveAreaName = provinceName + "-" + cityName;
                     $("#storeCodes").val(cityStore.storeCodes);
                     if ($("#storeCodes").val().trim().length === 0) {
                 		layer.tips('门店编码不能为空', '#add_store_codes_tips', {
     	                      tips: [2, '#ff4400']
     	                    });
     	                    return false;
                 	}
                 	if (!storeCodeRegex.test($("#storeCodes").val())){
                         layer.tips('门店编码格式不正确', '#add_store_codes_tips', {
                           tips: [2, '#ff4400']
                         });
                         return false;
                    }
                 	//修改时，只有第一个是有效的
                 	cityStore.storeCode = cityStore.storeCodes.split(",")[0];
                    console.log(cityStore);
                    $X("/wms/cityStoreService", "updateCityStore").callx({
                    	cityStore: cityStore,
                        onResult: function (data) {
                        	data = eval(data);
                        	$("#errorMessage").html(data.message);
                        	$.openx.tableRefresh(null, null, true);
                        	if(data.success==true){
                        		layer.closeAll();
                        	}
//                            layer.msg(data,{time:6000});
                        },
                        onError: function (error) {
                            var res = JSON.parse(error);
                            layer.msg(res.message)
                        }
                    });

                }
            });
        });
        $.ajaxSetup({async: false});
        initAreaSelect("add_province", "add_city", "district");
        //根据receiveAreaName初始化select下拉框
        if(row[0].receiveAreaName != undefined){
	        var name = row[0].receiveAreaName.split("-");
	        if(name[1] =="*"){
	            $('#add_province option').filter(function () {
	                return $(this).html() == name[0]; }).attr("selected", true);
	            $('#add_province').trigger("change");
	        }else{
	            $('#add_province option').filter(function () {
	                return $(this).html() == name[0]; }).attr("selected", true);
	            //自动触发onchange
	            $('#add_province').trigger("change");
	            $('#add_city option').filter(function () {
	                return $(this).html() == name[1]; }).attr("selected", true);
	        }
        }
        
        $("#storeCodes").val(row[0].storeCode);
        $("#add_id").val(row[0].id);
    });


};

function timeFormat(value, row) {
	if(value){
		return $.openx.formatDate(new Date(value));
	}
    
}


//删除操作
$.openx.delete = function () {
    $.openx.onClickDelete(function (row) {
    	var bean = new Object();
    	bean.storeCodes = $.openx.getArrays('storeCode',row);
        $X("/wms/cityStoreService", "deleteCityStore").callx({
        	bean: bean,
            onResult: function (data) {
                $.openx.tableRefresh(null, null, true);
                layer.msg("删除成功");
            },
            onError: function (error) {
                layer.msg("系统错误，请联系管理员");
            }
        });
    });
};


$.openx.query = function () {
    if($("#province option:selected").val()){
        var provinceName = $("#province option:selected").text();
    }
    if($("#city option:selected").val()){
        var cityName = $("#city option:selected").text();
    }else{
        var cityName="";
    }
    if($("#province option:selected").val()||$("#city option:selected").val()){
        $("#cityName").val(cityName)
    }else{
        $("#cityName").val("");
    }

    var data = $.openx.getJsonObj(queryForm);
    data.receiveAreaName = provinceName;
    param.bean = data;
    
    $.openx.tableRefresh(url, param);

}